package com.example.tests; 

import org.junit.*;
import static org.junit.Assert.*;

import com.example.common.SortedListOf;
import com.example.common.TestBase;
import com.example.common.User;
import com.example.pages.UsersPage;

public class RemoveAllUsers extends TestBase {
    @Test
    public void testRemoveAllUsers() throws Exception{
   
    	UsersPage userPage = goToMainPage().loginWithValidCredentials("admin",
				"admin").gotoUserManagementPage();

		int oldSize = userPage.getUserCount();
		int newSize;
		SortedListOf<User> oldUserList = userPage.getUsers();
	
    	if (oldSize == 1) {
    		int j = (int)Math.ceil(Math.random() * 5) + 1;
    	
    		for (int i = 0; i < j; i++) {
    			User user = new User().withName("user" + i).withEmail("user" + i + "@test.ru")
    					.withPassword("user" + i);
    			userPage.createUser(user);
    			user = null;
    		}
			newSize = userPage.getUserCount();
			assertEquals(newSize, oldSize + j);
			oldSize = newSize;
			oldUserList = userPage.getUsers();
    	}
    	
    	for (User user : oldUserList) {
    		if (!(user.name.equals("admin"))) userPage.deleteUser(user); 
    	}
    	newSize = userPage.getUserCount();
    	assertEquals(newSize, 1);
    	userPage.logout();
    }

     
}